package com.uaijug.dengue.app;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.uaijug.dengue.entidade.Foco;

public class ExibirFocoActivity extends Activity implements ViewSwitcher.ViewFactory {

	private ImageSwitcher imageSwitcher;
	private static final String URL = "http://10.0.2.2:8888/listaFocos";
	
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		Intent it = getIntent();
		
		final Foco foco = downloadFoco(URL, it.getLongExtra("id", 10));
		setContentView(R.layout.foco);
		
		popularTela(foco);
		
		// Configura o ImageSwitcher e os efeitos
		imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
		imageSwitcher.setFactory(this);
		imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
		imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));

		// Configura o adaptador da galeria
		Gallery g = (Gallery) findViewById(R.id.gallery);
		g.setAdapter(new AdaptadorFocoSwitcher(this, foco.getFotos(), new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)));
		
		g.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@SuppressWarnings("rawtypes")
			public void onItemSelected(AdapterView parent, View v, int posicao, long id) {
				// Avisa o ImageSwitcher que a imagem foi alterada
				Bitmap bitmap = BitmapFactory.decodeByteArray(foco.getFotos().get(posicao).getFotoData(), 0, foco.getFotos().get(posicao).getFotoData().length);
				BitmapDrawable bmd = new BitmapDrawable(bitmap);
				imageSwitcher.setImageDrawable(bmd);
			}
			@SuppressWarnings("rawtypes")
			public void onNothingSelected(AdapterView parent) {
			}
		});
	}
	
	private void popularTela(Foco foco) {
		TextView nomeCaptador = (TextView) findViewById(R.id.valueNomeCaptador);
        nomeCaptador.setText(foco.getNotificador().getNome());
        
        TextView latitude = (TextView) findViewById(R.id.valueLatitude);
        latitude.setText(String.valueOf(foco.getLatitude()));
        
        TextView longitude = (TextView) findViewById(R.id.valueLongitude);
        longitude.setText(String.valueOf(foco.getLongitude()));
	}

	// Implementa "ViewSwitcher.ViewFactory"
	public View makeView() {
		ImageView img = new ImageView(this);
		img.setBackgroundColor(0xFF000000);
		img.setScaleType(ImageView.ScaleType.FIT_CENTER);
		img.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		return img;
	}
	
	// Busca os focos do Servlet
	private Foco downloadFoco(String url, Long id) {
		Log.i(Constantes.DEBUG.LOG, "downloadFocos: " + url);
		try {
			
			URL u = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) u.openConnection();
			conn.setRequestMethod("POST");
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.setRequestProperty("Content-Type", "application/octet-stream");
			conn.connect();
			
			Foco foco = new Foco();
			foco.setId(id);
			
            ObjectOutputStream wr = new ObjectOutputStream(conn.getOutputStream());  
            wr.writeObject(foco); 
            wr.flush();
            wr.close();  

            ObjectInputStream ois = new ObjectInputStream(conn.getInputStream());  
            Foco focoRet = (Foco) ois.readObject();
            ois.close();
            conn.disconnect();
            
			return focoRet;
 
		} catch (MalformedURLException e) {
			Log.e(Constantes.DEBUG.LOG, e.getMessage(), e);
		} catch (IOException e) {
			Log.e(Constantes.DEBUG.LOG, e.getMessage(), e);
		} catch (Exception e) {
			Log.e(Constantes.DEBUG.LOG, e.getMessage(), e);
		}
		return null;
	}
}
